package com.ysyc.driver_ee.activity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.INaviInfoCallback;
import com.amap.api.navi.model.AMapNaviLocation;
import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.base.BaseActivity;
import com.ysyc.driver_ee.model.OrderForm;
import com.ysyc.driver_ee.tools.ScreenUtils;
import com.ysyc.driver_ee.tools.StringUtils;
import com.ysyc.driver_ee.widget.TitleView;

import java.util.List;

import butterknife.BindView;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/12 15:51<br>
 * 版本： v1.0<br>
 */
public class OrderNavigationActivity extends BaseActivity {
    @BindView(R.id.orderNavigationTitle)
    TitleView orderNavigationTitle;
    @BindView(R.id.orderNavigationMap)
    MapView mMapView;
    @BindView(R.id.navigationStartName)
    TextView navigationStartName;
    @BindView(R.id.navigationEndName)
    TextView navigationEndName;
    @BindView(R.id.orderFromSubmit)
    TextView orderFromSubmit;
    private UiSettings mUiSettings;//定义一个UiSettings对象
    AMap aMap;

    // 起点
    private LatLng mStartPoint;
    // 终点
    private LatLng mEndPoint;
    private LatLngBounds.Builder boundsBuilder;
    private OrderForm orderForm;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_navigation;
    }

    @Override
    protected void initView() {
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        orderNavigationTitle.setLeftImg(R.drawable.left_return_icon);
        orderNavigationTitle.setLeftImgClick(v -> finish());
        orderNavigationTitle.setLeftText("订单详情");
        orderForm = getIntent().getParcelableExtra("OrderForm");
        orderFromStatus(orderForm.status);
        mUiSettings = aMap.getUiSettings();//实例化UiSettings类对象
        mUiSettings.setZoomControlsEnabled(false);
        List<String> startingList = StringUtils.getListString(orderForm.startMap);
        List<String> endingList = StringUtils.getListString(orderForm.endMap);
        mStartPoint = new LatLng(Double.parseDouble(startingList.get(1)), Double.parseDouble(startingList.get(0)));
        mEndPoint = new LatLng(Double.parseDouble(endingList.get(1)), Double.parseDouble(endingList.get(0)));
        if (boundsBuilder == null) {
            //存放所有点的经纬度
            boundsBuilder = new LatLngBounds.Builder();
            boundsBuilder.include(mStartPoint);//把所有点都include进去（LatLng类型）
            boundsBuilder.include(mEndPoint);//把所有点都include进去（LatLng类型）
            final Marker marker = aMap.addMarker(new MarkerOptions().position(mStartPoint).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                    .decodeResource(getResources(), R.drawable.starting_point_trolley_icon))));
            final Marker marker1 = aMap.addMarker(new MarkerOptions().position(mEndPoint).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                    .decodeResource(getResources(), R.drawable.terminal_car_icon))));
            aMap.animateCamera(CameraUpdateFactory.newLatLngBoundsRect(boundsBuilder.build(), ScreenUtils.dip2px(OrderNavigationActivity.this, 50), ScreenUtils.dip2px(OrderNavigationActivity.this, 50), ScreenUtils.dip2px(OrderNavigationActivity.this, 300), ScreenUtils.dip2px(OrderNavigationActivity.this, 200)));
        }
    }


    public void navigationStartName(View view) {
        Poi start = new Poi("我的位置", new LatLng(location.getLatitude(), location.getLongitude()), "");
        Poi end = new Poi("货运起点（" + orderForm.startName + ")", new LatLng(mStartPoint.latitude, mStartPoint.longitude), "");
        jumpMap(start, end);

    }

    public void navigationEndName(View view) {
        Poi start = new Poi("我的位置", new LatLng(location.getLatitude(), location.getLongitude()), "");
        ;
        /**终点传入的是北京站坐标,但是POI的ID "B000A83M61"对应的是北京西站，所以实际算路以北京西站作为终点**/
        Poi end = new Poi("货运终点（" + orderForm.endName + ")", new LatLng(mEndPoint.latitude, mEndPoint.longitude), "");
        jumpMap(start, end);
    }


    private void jumpMap(Poi start, Poi end) {
        AmapNaviPage.getInstance().showRouteActivity(this, new AmapNaviParams(start, null, end, AmapNaviType.DRIVER), new INaviInfoCallback() {
            @Override
            public void onInitNaviFailure() {

            }

            @Override
            public void onGetNavigationText(String s) {

            }

            @Override
            public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

            }

            @Override
            public void onArriveDestination(boolean b) {


            }

            @Override
            public void onStartNavi(int i) {

            }

            @Override
            public void onCalculateRouteSuccess(int[] ints) {

            }

            @Override
            public void onCalculateRouteFailure(int i) {

            }

            @Override
            public void onStopSpeaking() {

            }

            @Override
            public void onReCalculateRoute(int i) {

            }

            @Override
            public void onExitPage(int i) {

            }

            @Override
            public void onStrategyChanged(int i) {

            }

            @Override
            public View getCustomNaviBottomView() {
                return null;
            }

            @Override
            public View getCustomNaviView() {
                return null;
            }

            @Override
            public void onArrivedWayPoint(int i) {

            }
        });
    }

    //0为未接单 1为已接单(即待装货) 2为到达装货地(即装货中) 3为已装货(即正在送货中) 4为已完成
    private void orderFromStatus(Integer status) {
        if (0 ==status){
            orderFromSubmit.setText("确认到达装货地点");
        }else if (1 ==status){
            orderFromSubmit.setText("确认已装货");
        }else if (2 ==status){
            orderFromSubmit.setText("确认到达装货地点");
        }else if (3 ==status){
            orderFromSubmit.setText("确认已完成");
        }else if (4 ==status){
            orderFromSubmit.setText("确认订单完成");
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mMapView.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        if (mMapView != null) {
            mMapView.onDestroy();
        }
    }


}
